﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FinalProject.Services.Interfaces;
using FinalProject.Entities;
using FinalProject.Web.ViewModels;

namespace FinalProject.Web.Controllers
{
    public class TicketController : Controller
    {
        private readonly ITicketService _ticketService;
        private readonly IItemService _itemService;
        private readonly IClientService _clientService;

        public TicketController(ITicketService ticketService, IItemService itemService, IClientService clientService)
        {
            this._ticketService = ticketService;
            this._itemService = itemService;
            this._clientService = clientService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create(int id = 0)
        {
            try
            {
                CreateTicketViewModel ticketViewModel = new CreateTicketViewModel();
                ticketViewModel.ClientId = id;
                var items = _itemService.GetAll().Entities.ToList();
                ticketViewModel.CreateInventoryItems(items);
                ticketViewModel.ClientName = _clientService.GetById(id).PersonName.FirstName;
                return View(ticketViewModel);
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, "Ticket", "Create");
                return View("Error", model);
            }
        }

        [HttpPost]
        public ActionResult Create(CreateTicketViewModel ticketViewModel)
        {
            Ticket ticket = new Ticket();
            ticket.ClientId = ticketViewModel.ClientId;
            ticket.Name = ticketViewModel.Name;
            ticket.TicketItems = ticketViewModel.TicketItems;
            _ticketService.Create(ticket);

            //var ticketItem = new TicketItem();

            //remove next line once method is completed.
            throw new NotImplementedException();
        }
    }
}
